<script setup>
// TODO: 编辑
import { ref, watch, defineEmits, defineProps } from 'vue'

// 定义props和emits
const props = defineProps({
  item: Object // 从父组件接收的编辑数据
});

const emit = defineEmits(['save','close'])

// 弹框开关
const dialogVisible = ref(false)

// 表单数据
const formData = ref({
  name: '',
  place: ''
})

// 监听传入的 item 数据，并同步到 formData
watch(() => props.item, (newItem) => {
  if (newItem) {
    formData.value = { ...newItem };
    dialogVisible.value = true;  // 打开弹窗
  }
}, { immediate: true });

// 处理确认按钮点击事件
const handleSave = () => {
  emit('save', formData.value);  // 触发保存事件，将修改后的数据传递回父组件
  dialogVisible.value = false;   // 关闭弹窗
};

// 处理取消按钮点击事件
const handleCancel = () => {
  emit('close');  // 触发关闭事件
  dialogVisible.value = false;   // 关闭弹窗
};

</script>

<template>
  <el-dialog v-model="dialogVisible" title="编辑" width="400px">
    <el-form label-width="50px">
      <el-form-item label="姓名">
        <el-input v-model="formData.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="籍贯">
        <el-input v-model="formData.place" placeholder="请输入籍贯" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.el-input {
  width: 290px;
}
</style>
